//
//  MessageVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/19.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit
import Dialog

class MessageCell: UITableViewCell {
	@IBOutlet weak var iconIV: UIImageView!
	@IBOutlet weak var tipsLabel: UILabel!
	@IBOutlet weak var contentLabel: UILabel!
	
	var message: Message!{
		didSet{
			iconIV.image = message.kind.icon
			tipsLabel.attributedText = message.attributedTips
			contentLabel?.text = message.content
		}
	}
}

class MessageVC: UITableViewController {

	@IBOutlet weak var navClearBtn: UIButton!
	
	fileprivate var messages: [Message] = []{
		didSet{
			navClearBtn.isHidden = messages.isEmpty
		}
	}
	
    override func viewDidLoad() {
        super.viewDidLoad()

		tableView.rowHeight = UITableViewAutomaticDimension
		
		let m1: [Message] = (0...2).map{i in
			let m = Message()
			m.kind = .dropCommented
			m.content = "这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容这是评论内容"
			return m
		}
		let m2: [Message] = (0...2).map{i in
			let m = Message()
			m.kind = .commentLiked
			return m
		}
		let m3: [Message] = (0...2).map{i in
			let m = Message()
			m.kind = .dropLiked
			return m
		}
		let m4: [Message] = (0...2).map{i in
			let m = Message()
			m.kind = .commentReplied
			m.content = "这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容这是回复内容"
			return m
		}
		messages.append(contentsOf: m1)
		messages.append(contentsOf: m2)
		messages.append(contentsOf: m3)
		messages.append(contentsOf: m4)
    }
    // MARK: - Table view data source

	override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
		return .zero
	}
	
	override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
		return .zero
	}
	
    override func numberOfSections(in tableView: UITableView) -> Int {
		return messages.isEmpty ? 1 : 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return !messages.isEmpty
			? (section == 0 ? messages.count : 1)
			: 1
    }

	
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		
		if messages.isEmpty || indexPath.section == 1 {
			let cell = tableView.dequeueReusableCell(withIdentifier: "LoadMore", for: indexPath)
			
			return cell
		}
		
		let message = messages[indexPath.row]
		let isCommentMessage = message.kind == .commentReplied || message.kind == .dropCommented
		let reuseId = isCommentMessage ? "Comment" : "Like"
		let cell = tableView.dequeueReusableCell(withIdentifier: "MessageCell\(reuseId)", for: indexPath) as! MessageCell
		cell.message = message
		cell.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(cellLongPressed(_:))))
		
        return cell
    }
	
	@IBAction func navClear(_ sender: UIButton){
		Dialog.default(title: .init("确定要删除所有消息?"), actions: [
			.init(title: "删除", style: .destructive, handler: { _ in
				self.messages.removeAll()
				self.tableView.deleteSections(.zero, with: .bottom)
			}),
            .init(title: "取消", style: .cancel)
		])
	}

	@objc fileprivate func cellLongPressed(_ sender: UIGestureRecognizer){
		guard sender.state == .began else {
			return
		}
		let configuration  = Dialog.Configuration()
		configuration.isBackgroundViewUserInteractionEnabled = true
		Dialog.default(actions: [
			.init(title: "删除", style: .destructive, handler: { _ in
				if let cell = sender.view as? UITableViewCell, let indexPath = self.tableView.indexPath(for: cell){
					self.messages.remove(at: indexPath.row)
					self.tableView.deleteRows(at: [indexPath], with: .automatic)
				}
			}),
			.init(title: "举报"),
			.init(title: "屏蔽")
		], configuration: configuration)
	}
}
